//package com.service.schedule;
//
//import javax.annotation.PostConstruct;
//
//import org.quartz.Scheduler;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.quartz.CronTrigger;
//import org.quartz.Scheduler;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.model.schedule.ScheduleJob;
//import com.service.schedule.util.ScheduleUtils;
//
//import javax.annotation.PostConstruct;
//import java.util.List;
//import java.util.Map;
//
//@Service
//public class ScheduleJobService {
//
//	@Autowired
//	private Scheduler scheduler;
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 项目启动时，启动定时任务
//	 * @date 2020/11/13 15:08
//	 * @params []
//	 **/
//	@PostConstruct
//	public void init() {
//		List<ScheduleJob> scheduleJobList = baseMapper.selectList(null);
//		for (ScheduleJob scheduleJob : scheduleJobList) {
//			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
//			// 如果不存在，则创建
//			if (cronTrigger == null) {
//				ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
//			} else {
//				ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
//			}
//		}
//	}
//
//	/**
//	 * @return java.util.List<com.wph.task.model.ScheduleJob>
//	 * @author chengpunan
//	 * @desc 查询定时任务列表
//	 * @date 2020/11/12 16:20
//	 * @params [params]
//	 **/
//	public List<ScheduleJob> selectDetailPage(Map<String, Object> params) {
//		List<ScheduleJob> list = baseMapper.selectDetailPage(params);
//		return list;
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 创建定时任务，并启动定时任务
//	 * @date 2020/11/13 8:53
//	 * @params [scheduleJob]
//	 **/
//	@Transactional(rollbackFor = Exception.class)
//	public void insert(ScheduleJob scheduleJob) {
//		scheduleJob.setJobId(com.wph.core.utils.StringUtils.uuid());
//		scheduleJob.setStatus(Constant.SCHEDULE_STATUS_NORMAL);
//		scheduleJob.setCreator(LoginUserHolder.getUserId());
//		scheduleJob.setCreateTime(DateUtils.getDate());
//		baseMapper.insert(scheduleJob);
//		ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 修改定时任务
//	 * @date 2020/11/13 14:12
//	 * @params [scheduleJob]
//	 **/
//	@Transactional(rollbackFor = Exception.class)
//	public void modify(ScheduleJob scheduleJob) {
//		// 删除之前的定时任务
//		ScheduleUtils.deleteScheduleJob(scheduler, scheduleJob.getJobId());
//		ScheduleJob scheduleJobParam = baseMapper.selectById(scheduleJob);
//		scheduleJobParam.setBeanName(scheduleJob.getBeanName());
//		scheduleJobParam.setMethodName(scheduleJob.getMethodName());
//		scheduleJobParam.setCronExpression(scheduleJob.getCronExpression());
//		scheduleJobParam.setParams(scheduleJob.getParams());
//		scheduleJobParam.setRemark(scheduleJob.getRemark());
//		scheduleJobParam.setRunStatus(Constant.SCHEDULE_RUN_STATUS_SUCCESS);
//		baseMapper.updateById(scheduleJobParam);
//		// 插入新的定时任务
//		ScheduleUtils.createScheduleJob(scheduler, scheduleJobParam);
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 删除定时任务
//	 * @date 2020/11/13 15:04
//	 * @params [jobIds]
//	 **/
//	@Transactional(rollbackFor = Exception.class)
//	public void delete(List<String> jobIds) {
//		for (String jobId : jobIds) {
//
//			ScheduleUtils.deleteScheduleJob(scheduler, jobId);
//			baseMapper.deleteQRTZ_CRON_TRIGGERS(jobId);
//			baseMapper.deleteQRTZ_JOB_DETAILS(jobId);
//			baseMapper.deleteQRTZ_TRIGGERS(jobId);
//		}
//		baseMapper.deleteBatchIds(jobIds);
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 立即执行定时任务
//	 * @date 2020/11/13 15:32
//	 * @params [jobId]
//	 **/
//	public void run(String jobId) {
//		ScheduleUtils.run(scheduler, baseMapper.selectById(jobId));
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 暂停定时任务
//	 * @date 2020/11/13 15:41
//	 * @params [jobIds]
//	 **/
//	@Transactional(rollbackFor = Exception.class)
//	public void pause(String jobId) {
//		ScheduleUtils.pauseJob(scheduler, jobId);
//		ScheduleJob scheduleJob = baseMapper.selectById(jobId);
//		if (scheduleJob == null) {
//			throw new ServiceException(HttpStatus.GLOBAL_EXCEPTION_CODE, "定时任务信息不存在!");
//		}
//		scheduleJob.setStatus(Constant.SCHEDULE_STATUS_PAUSE);
//		baseMapper.updateById(scheduleJob);
//	}
//
//	/**
//	 * @return void
//	 * @author chengpunan
//	 * @desc 暂停定时任务
//	 * @date 2020/11/13 15:57
//	 * @params [param]
//	 **/
//	@Transactional(rollbackFor = Exception.class)
//	public void resume(String jobId) {
//		ScheduleUtils.resumeJob(scheduler, jobId);
//		ScheduleJob scheduleJob = baseMapper.selectById(jobId);
//		if (scheduleJob == null) {
//			throw new ServiceException(HttpStatus.GLOBAL_EXCEPTION_CODE, "定时任务信息不存在!");
//		}
//		scheduleJob.setStatus(Constant.SCHEDULE_STATUS_NORMAL);
//		baseMapper.updateById(scheduleJob);
//	}
//}
